<?php
/*
 * @ Nguyễn Như Khang
 * nhukhang095@gmail.com
 */
class BaseDAO{
	var $utils ;
	var $db ;
	//var $log;

	function BaseDAO()
	{
		$this->utils = new CommonUtils;
		$this->db = new DBUtils;
		///$this->log  = new Logging();
	}

	function create($table, $fields, $values)
	{
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
			
		$query = "INSERT INTO " . $table . " (";
		$queryValues = "";
		$bFirst = true;
		for($i = 0; $i < count($fields); $i++) {
			if (!$bFirst) {
				$query .= ", ";
			}
			$query .= $fields[$i];
			$bFirst = false;
		}
		$bFirst = true;
		for($i = 0; $i < count($values); $i++) {
			if (!$bFirst) {
				$queryValues .= ", ";
			}
			$queryValues .= "'" . $values[$i] . "'";
			$bFirst = false;
		}
		$query .= ") VALUES (" . $queryValues . ")";
		$res = $this->db->executeQuery($query);
		if (isset($res) ) {
			$result= mysql_query ('SELECT LAST_INSERT_ID()') ;
			if ($result) {
				$nrows = mysql_num_rows($result);
				$row = mysql_fetch_row($result);
				$lastID = $row[0];
			}
		}
		$res=$lastID;
		$this->db->closeConnection();
		return $res;
	}
	function del($table, $id, $idFldName = 'id'){
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$query = "DELETE FROM $table WHERE $idFldName = '" . $id . "'";
		$res = $this->db->executeQuery($query);
		$this->db->closeConnection();
		return $res;
	}

	function executeUpdate($table, $query){
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$res = $this->db->executeQuery($query);
		$this->db->closeConnection();
		return $res;
	}

	function update($table, $fields, $values, $idFldName = 'id')
	{
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$query = "UPDATE $table SET ";
		$bFirst = true;
		for($i = 0; $i < count($fields); $i++) {
			if (!$bFirst) {
				$query .= ", ";
			}
			$query .= $fields[$i] . " = '" . $values[$i] . "'";
			$bFirst = false;
		}
		$query .= " WHERE $idFldName = '". $values[0] . "'";

		$res = $this->db->executeQuery($query);
		$this->db->closeConnection();
		return $res;
	}
	function getItem($table, $fields, $id, $idFldName = 'id'){
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$query = "SELECT * " . " FROM " . $table . " WHERE  $idFldName = '$id'";
		$result  = $this->db->executeQuery($query);

		if ($result) {
			$row = $this->db->getReader($result) ;
			$enquiry = array();
			foreach($fields as $f) {
				$enquiry[$f] = $row[$f];
			}

		}
		$this->db->closeConnection();
		return $enquiry;
	}

	function checkDuplicate($table,$where='',$name, $id){
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$num=0;
		$query = "SELECT count(*) as num " . " FROM " . $table . " WHERE ";
		if($where!=''){
			$query .= " ( ".$where ." ) AND id!='".$id."'"	;
		}else{
			$query .=" name='".$name."' AND id!='".$id."'";
		}
		$result=  $this->db->executeQuery($query) ;
		if ($result) {
			$row = mysql_fetch_row($result);
			$num= $row[0];
		}
		$this->db->closeConnection();
		return $num;
	}

	function getItems($table, $fields, $queryFilter = '', $orderby = '', $begin = -1, $limit = ''){
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$query = "SELECT * " . " FROM " . $table . " WHERE  1 = 1 ";
		if (isset($queryFilter) && $queryFilter != '') {
			$query .=  " AND " . $queryFilter;
		}
		if (isset($orderby) && $orderby != '') {
			$query .= " $orderby";
		}
		if (isset($begin) && $begin >= 0) {
			$query .= "  LIMIT $begin, $limit";
		}
		$result  = $this->db->executeQuery($query);
		if ($result) {
			$returnres = array();
			while ($row = $this->db->getReader($result)) {
				$enquiry = array();
				foreach($fields as $f) {
					if($f == "updatedby"){
						$mRresult = $this->db->executeQuery("select fullname from user where id = '".$row[$f]."'");
						if($mRresult){
							$mRow = mysql_fetch_row($mRresult);
							if($mRow){
								$enquiry[$f] = $mRow[0];
							}
						}
					}else{
						$enquiry[$f] = $row[$f];
					}
				}
				$returnres[] = $enquiry;
			}
			$this->db->closeConnection();
			return $returnres;
		}
		return false;

	}

	function getTotalItems($table, $queryFilter){
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$query = "SELECT COUNT(*) as totalItem " . " FROM " . $table;
		if (isset($queryFilter)) {
			$query .= " WHERE " . $queryFilter;
		}
		$result  = $this->db->executeQuery($query);
		if ($result) {
			$row = $this->db->getReader($result);
			return $row['totalItem'];
		}
		$this->db->closeConnection();
		return 0;
	}
}
?>